<template>
	<view>
		<view style="font-weight: 600;">
			<button @click="czTest">当前代码环境 {{ currentProfile }}</button>
		</view>
		<!-- #ifdef MP-TOUTIAO -->
		<textarea @input="inputPay" maxlength="30000" placeholder="输入支付参数"/>
		<button @click="testPayNewTT">通用支付测试</button>
		order_id:<textarea v-model="order_id" placeholder="输入order_id" />
		order_token:<textarea v-model="order_token" placeholder="输入order_token" />
		<!-- v-if="shareBatchId" -->
		<button v-if="shareBatchId" @click="openCzPage">充值活动</button>
		<!-- 活动id -->
		<view v-if="showCzPage" class="mask">
			<view class="mask-content">
				<view class="mask-title">
					输入活动id
					<input class="mask-input" v-model="activityId" />
				</view>
				<button class="mask-btn" @click="jumpActivityPage">确定</button>
			</view>

		</view>
		<button v-if="darenChoose" @click="handleJumpActive">私域专题页</button>
		<button @click="czTest">抖音充值测试</button>
		<button @click="toUser">跳转抖音主页</button>
		<button @click="czTestIOS">抖音充值IOS</button>
		<!-- #endif -->
		<button open-type="openProfile" user="2670056352">打开用户主页</button>
		<!-- 重复跳转触发两次用户主页 -->
		<cover-view style="left: 200rpx;top: 200rpx;z-index: 999;">
			<button :plain="true" hover-class="none"
				style="margin-top: 44rpx;border: 0px solid #000000;color: white;padding: 0;margin-bottom: 20rpx;"
				class="sideBarItem" aria-role="button" open-type="openProfile" user="2670056352">
				<cover-image style="width: 78rpx;height: 78rpx;"
					src="https://res.qiguoread.com/staticResources/fellow_townsman/image/uni/icon_user.png"></cover-image>
			</button>
		</cover-view>
		<!-- 不使用cover-view则正常 -->
		<view style="left: 200rpx;top: 200rpx;z-index: 999;">
			<button :plain="true" hover-class="none"
				style="margin-top: 44rpx;border: 0px solid #000000;color: white;padding: 0;margin-bottom: 20rpx;"
				class="sideBarItem" aria-role="button" open-type="openProfile" user="2670056352">
				<cover-image style="width: 78rpx;height: 78rpx;"
					src="https://res.qiguoread.com/staticResources/fellow_townsman/image/uni/icon_user.png"></cover-image>
			</button>
		</view>
		<button open-type="im" :data-im-id="imId" @im="imCallback" @error="onImError">IM客服</button>
		<button @click="clearTimes">清除次数</button>
		<button @click="toWx">跳转微信小程序</button>
		<button @click="changeCz">充值:{{ openCz? '已开启': '已关闭' }}</button>
		<button @click="changeBatchId">分享携带批次号:{{ shareWithBatchId? '已开启': '已关闭' }}</button>
		<view>当前入参:{{ pathOptions }}</view>
		<button @click="openAd">开启激励视频</button>
		<input placeholder="输入链接" v-model="webviewUrl" />
		<button @click="handleJumpWebview">webview跳转</button>
		<button @click="changePROFILE">当前环境：{{ PROFILE }}</button>
		<button @click="delPROFILE">清空环境信息</button>
		<button @click="changeattributeServerApi">开启上报打印：{{ attributeServerApi }}</button>
		<button @click="relauch">重新进入小程序</button>
		<view>userInfo:{{ JSON.stringify(userInfo) }}</view>
		<view>scene:{{ scene }}</view>
		<view>batchId:{{ batchId }}</view>
		<view>hasBuy:{{ JSON.stringify(hasBuy) }}</view>
		<view>accountInfo:{{ JSON.stringify(accountInfo) }}</view>
		<input placeholder="输入fakeId" v-model="isInput" />
		<button @click="enterJuji">进入某部剧集</button>
		<button style="font-size: 22rpx;" @click="darenChoose1">进入达人选剧(测试专用功能不影响线上逻辑,使用完毕后需要手动关闭)</button>
		<button @click="darenChooseClose">退出达人选剧</button>
		<button @click="batchEnter">批次号进入</button>
		<button @click="copyBatchId">复制batchId</button>
		<button @click="copyUserInfo">复制userInfo</button>
		<button @click="copyPathOptions">复制入参</button>
		<input placeholder="输入batchId" v-model="batchIdInput" />
		<button @click="keepBatchId">配置spreadChannelId</button>
		<button @click="clearBatchId">清除spreadChannelId</button>
		<button @click="clearCopyLinkTime">清除copyLinkTimequkouling</button>
		<!-- #ifdef MP-TOUTIAO -->
		<moneyPopupTT @czSuccess="czSuccess" @getListSuccess="getListSuccess" v-if="showMoney" @isClose="moneyClose"
			:balance="balance"></moneyPopupTT>
		<!-- #endif -->
		<button @click="handleAddMess">订阅消息唤醒</button>
		<input placeholder="设置分享的批次ID" v-model="batchIdGroup" />
		<view>当前已设置的批次：{{ nowBatchId }}</view>
		<button @click="handleKeepBatchId">保存当前的批次ID</button>


		<view>{{ adMess }}</view>
		<button @click="copyMessSet(adMess)">保存adMess</button>
		<view>{{ adMessLinkKey }}</view>
		<button @click="copyMessSet(adMessLinkKey)">保存adMessLinkKey</button>
		<view>{{ adMessLinkKeyAll }}</view>
		<button @click="copyMessSet(adMessLinkKeyAll)">保存adMessLinkKeyAll</button>
		<view>本地缓存信息:</view>
		<view v-for="(item, index) in allStorageMess" :key="index" class="line-mess">
			<text>{{ index }}:</text>
			<view class="right-box">{{ item }}</view>
		</view>
		<button open-type="share" data-channel="video">拍视频</button>
		<view v-if="!hideBox">
			<text>2121212dasdasdasda</text>
			<follow-service followed-id="2670056352" desc="关注我，了解特效师的工作日常" @follow="followBack"
				@error="followError"></follow-service>
		</view>

	</view>
</template>



<script>
import moneyPopupTT from '../../components/moneyPopupTT.vue';
let rewardedVideoAd = null;
export default {
	components: {
		moneyPopupTT
	},
	data() {
		return {
			imId: '22329354662',
			showMoney: false,
			order_id: '',
			order_token: '',
			pathOptions: '',
			attributeServerApi: false,
			PROFILE: '',
			isInput: '',
			userInfo: {},
			scene: '',
			batchId: '',
			hasBuy: '',
			accountInfo: '',
			batchIdInput: '',
			openCz: false,
			shareWithBatchId: getApp().globalData.shareWithBatchId,
			webviewUrl: '',
			batchIdGroup: '',
			nowBatchId: '',
			allStorageMess: [],
			adMess: '',
			adMessLinkKey: '',
			adMessLinkKeyAll: '',
			hideBox: false,
			showCzPage: false,
			activityId: '',
			shareBatchId:getApp().globalData.shareBatchId,
			darenChoose: '',
			currentProfile:null,
			payJson: ''
		}
	},
	onLoad() {
		if(uni.getStorageSync('currentProfile')) this.currentProfile = uni.getStorageSync('currentProfile');
		this.darenChoose = getApp().globalData.darenChoose
		this.adMess = uni.getStorageSync('adMess')
		this.adMessLinkKey = uni.getStorageSync('adMessLinkKey')
		this.adMessLinkKeyAll = uni.getStorageSync('adMessLinkKeyAll')
		// uni.setClipboardData({
		// 	data:'小伙发明“小钢炮”驱鼠，一炮就把洞里祸害干掉，太痛快了"集结吧光合创作者 "乡村军仔农村生活 "发明 https://v.kuaishou.com/enTFWS 复制此消息，打开【快手】直接观看！'
		// })
		uni.hideToast();
		this.openCz = wx.getStorageSync('openCz') || false;
		// this.shareWithBatchId = wx.getStorageSync('shareWithBatchId') || false;
		this.shareWithBatchId = getApp().globalData.shareWithBatchId || false;
		this.nowBatchId = uni.getStorageSync('nowBatchId') || ''
		this.pathOptions = wx.getStorageSync('enterPath');
		this.attributeServerApi = getApp().globalData.attributeServerApi;
		this.PROFILE = getApp().globalData.PROFILE;
		this.userInfo = uni.getStorageSync('userInfo') || {};
		this.scene = getApp().globalData.scene;
		this.batchId = getApp().globalData['batchId'];
		this.shareBatchId = getApp().globalData.shareBatchId
		this.hasBuy = uni.getStorageSync('hasBuy');
		//#ifdef MP-KUAISHOU
		this.accountInfo = uni.getAccountInfoSync();
		//#endif
		//#ifdef MP-TOUTIAO
		if (tt.getEnvInfoSync) this.accountInfo = tt.getEnvInfoSync();
		//#endif


		// rewardedVideoAd = uni.createRewardedVideoAd({
		// 	type: 100011056,
		// 	unitId: 100014857,
		// 	adUnitId: '100014857'
		// });
		// rewardedVideoAd.onLoad((loadCode) => {
		// 	// uni.showToast({
		// 	// 	title:'激励视频广告加载成功' + loadCode,
		// 	// 	icon:'none'
		// 	// })
		// 	console.log('onLoad event emit', loadCode);
		// });
		// rewardedVideoAd.onError(({ errCode }) => {
		// 	// uni.showToast({
		// 	// 	title:'激励视频广告报错' + errCode,
		// 	// 	icon:'none'
		// 	// })
		// 	console.log('onError event emit', errCode);
		// });
		// rewardedVideoAd.onClose(({ isEnded }) => {
		// 	// uni.showToast({
		// 	// 	title:'激励视频广告已关闭' + isEnded,
		// 	// 	icon:'none'
		// 	// })
		// 	console.log('onClose event emit', isEnded);
		// });
		this.getStorageInfoMess()
	},
	onUnload() {
		// rewardedVideoAd.destroy();
		// rewardedVideoAd = null;
	},
	onShow() {
		this.getFollowCheckStatus()
	},
	onShareAppMessage: function () {
		var shareJson = {
			path: '/pages/toTest/toTest',
			data: {
				page: 'mine',
				sharePosition: '/pages/bg_watch/bg_watch',
				from: '/position/share/share',
				sharePage: '/pages/toTest/toTest'
			}
		};

		// 达人分享批次
		console.log(getApp().globalData.shareBatchId)
		if (getApp().globalData.shareBatchId) {
			shareJson.channel = 'video'
			shareJson.extra = {
				hashtag_list: ['精彩短剧热播中👈']
			}
		}
		shareJson.title = '精彩短剧热播中👈'

		return this.formatShare(shareJson);
	},
	methods: {
		inputPay(e) {
			console.log(e)
			this.payJson = e.detail.value
		},
		testPayNewTT() {
			if(!this.payJson) return
			let string = JSON.parse(this.payJson)
			let {data, byteAuthorization} = string
			console.log(string, typeof(string))
			tt.requestOrder({
				data: data,
				byteAuthorization: byteAuthorization,
				success(res) {
					console.log(res)
					if(res.orderId) {
						tt.getOrderPayment({
							orderId: res.orderId,
							success(res) {
								console.log('支付成功的回调', res)
							},
							fail(err) {
								console.log('支付失败的回调', err)
							}
						})
					}
					
				},
				fail(err) {
					console.log('订单失败的回调', err)
				}
			})
		},
		handleJumpActive() {
			uni.navigateTo({
				url: `/pages/specialPage/specialPage`
			})
		},
		openCzPage() {
			this.showCzPage = true
		},
		jumpActivityPage() {
			this.zwyRequest({
        url: `/cdp/spread_material/expert_share/getTopUpActivityId?id=${this.activityId}`,
        method: "get",
        data: {},
      }).then((res) => {
        console.log(res, "res11111111");
				uni.navigateTo({
				url: `/pages/recharge/recharge?id=${res}&batchId=${this.shareBatchId}`
			});
      });
			
		},
		followError(e) {
			console.log(e)
		},
		followBack() {
			console.log('12121212')
			this.hideBox = true
		},
		copyMessSet(val) {
			uni.setClipboardData({
				data: val
			})
		},
		imCallback(e) {
			console.log('客服回调信息', e)
		},
		onImError(e) {
			console.log('客服错误', e)
		},
		getStorageInfoMess() {
			try {
				let keys = []
				let obj = {} // 存储的数据
				const res = uni.getStorageInfoSync()
				keys = res.keys
				keys.forEach(key => {
					let val = uni.getStorageSync(key) || ''
					if (typeof val === 'object') {
						obj[key] = JSON.stringify(val)
					} else {
						obj[key] = val
					}
				})
				this.allStorageMess = obj
			} catch (error) {
				console.log(error)
			}
		},
		clearTimes() {
			uni.setStorageSync('hasShowKb', false);
			uni.removeStorageSync('viewGuidePopup');
			uni.removeStorageSync('adLockCount');
			uni.showToast({
				title: `清除成功`,
				icon: 'none'
			})
		},
		// 设置固定批次ID并开启分享携带
		handleKeepBatchId() {
			if (!this.batchIdGroup) {
				uni.showToast({
					title: `请输入批次ID`,
					icon: 'none'
				})
				return
			}

			uni.showToast({
				title: `保存成功`,
				icon: 'none'
			})
			getApp().globalData.batchId = this.batchIdGroup
			uni.setStorageSync('nowBatchId', this.batchIdGroup)
			this.shareWithBatchId = true
			getApp().globalData.shareWithBatchId = true
			wx.setStorageSync('shareWithBatchId', this.shareWithBatchId);
			setTimeout(() => {
				uni.navigateBack({
					delta: 1,
					fail: res => {
						uni.switchTab({
							url: '/pages/fx_xjc/fx_xjc'
						});
					}
				})
			}, 2000)
		},
		getFollowCheckStatus() {
			// #ifdef MP-TOUTIAO
			console.log('res========1212121212', tt.checkFollowAwemeState)
			tt.checkFollowAwemeState({
				success: (res) => {
					console.log('res========', res)
				},
				fail: (err) => {
					console.log('error========', res)
				}
			})
			// #endif
		},
		czTestIOS() {
			this.showMoney = true;
		},
		// 签到
		async signStatusChange(type) {
			try {
				if (type === 'click') {
					uni.showToast({
						title: `今天已签到`,
						icon: 'none'
					})
					return
				}
				this.addDingMess()

				let res = await this.zwyRequest({
					url: '/welfare/sign_in',
					method: 'PUT'
				})
			} catch (error) {
				console.log(error)
			}
		},
		// 订阅上报
		async addDingMess() {
			try {
				await this.zwyRequest({
					url: '/mp-api/user_one_time_subscribe/batch',
					method: 'POST',
					data: {
						sourceType: 3,
						sourceId: 0,
						sourceDesc: '',
						templates: [{
							templateId: getApp().globalData.extConfig['subscribeId6'],
							templateType: 3
						}]
					}
				})
				// console.log(12)
				// this.handleAddMess()
			} catch (error) {
				console.log(error)
			}
		},
		// 添加订阅提醒
		async handleAddMess() {
			try {
				this.signStatusChange()
				uni.requestSubscribeMessage({
					tmplIds: [getApp().globalData.extConfig['subscribeId6']],
					success: (res) => {
						if (res[getApp().globalData.extConfig['subscribeId6']] === 'ACCEPT') {
							uni.showToast({
								title: '订阅消息已开启',
								icon: 'none'
							})
						} else if (res[getApp().globalData.extConfig['subscribeId6']] === 'REFUSE') {
							uni.showToast({
								title: '请在程序设置中开启接收订阅',
								icon: 'none'
							})
						}
					},
					fail: (error) => {
						console.log('error', error)
					},
					complete: (end) => {
						console.log('end', end)
					}
				})
			} catch (error) {
				console.log(error)
			}
		},
		delPROFILE() {
			uni.removeStorageSync('PROFILE');
		},
		toUser() {
			console.log('toUser');
			tt.openAwemeUserProfile({
				success: (res) => {
					console.log('success', res);
				},
				fail: res => {
					console.log('err', res);
				}
			});
		},
		handleJumpWebview() {
			if (!this.webviewUrl) {
				uni.showToast({
					title: '请输入链接',
					icon: 'none'
				})
				return
			}
			uni.navigateTo({
				url: `../webview/webview?src=${this.webviewUrl}`
			})
		},
		toWx() {
			uni.navigateTo({
				url: '../toWx/toWx'
			})
		},
		czTest() {
			console.log('this.order_id', this.order_id);
			console.log('this.order_token', this.order_token);
			uni.pay({
				service: 5,
				orderInfo: {
					order_id: this.order_id,
					order_token: this.order_token
				},
				success(res) {
					console.log('successres', res);
					if (res.code === 0) {
						// 支付成功处理逻辑，只有res.code=0时，才表示支付成功
						// 但是最终状态要以商户后端结果为准
					}
				},
				fail(res) {
					console.log('failres', res);
					// handle fail
				},
			});

		},
		changeBatchId() {
			this.shareWithBatchId = !this.shareWithBatchId;
			getApp().globalData.shareWithBatchId = !getApp().globalData.shareWithBatchId
			wx.setStorageSync('shareWithBatchId', this.shareWithBatchId);
		},
		changeCz() {
			this.openCz = !this.openCz;
			wx.setStorageSync('openCz', this.openCz);
		},
		openAd() {
			rewardedVideoAd.show()
				.catch(() => {
					rewardedVideoAd.load()
						.then(() => rewardedVideoAd.show())
						.catch(err => {
							uni.showToast({
								title: '激励视频广告显示失败' + err,
								icon: 'none'
							})
							console.log('激励视频广告显示失败', err);
						});
				});
		},
		relauch() {
			uni.reLaunch({
				url: '../fx_xjc/fx_xjc'
			})
		},
		changeattributeServerApi() {
			this.attributeServerApi = !this.attributeServerApi;
			getApp().globalData.attributeServerApi = this.attributeServerApi;
		},
		changePROFILE() {
			console.log('changePROFILE', this.PROFILE);
			if (this.PROFILE == 'online') this.PROFILE = 'dev';
			else if (this.PROFILE == 'dev') this.PROFILE = 'qa';
			else if (this.PROFILE == 'qa') this.PROFILE = 'online';
			getApp().globalData.PROFILE = this.PROFILE;
			uni.setStorageSync('PROFILE', this.PROFILE)
		},
		enterJuji() {
			let tempUrl = `/pages/video/video?fakeId=${this.isInput ? this.isInput : 'BWG9'}`;
			console.log('tempUrl', tempUrl);
			uni.navigateTo({
				url: tempUrl
			})
		},
		copyPathOptions() {
			uni.setClipboardData({
				data: JSON.stringify(this.pathOptions)
			})
		},
		copyUserInfo() {
			uni.setClipboardData({
				data: JSON.stringify(this.userInfo)
			})
		},
		copyBatchId() {
			uni.setClipboardData({
				data: this.batchId
			})
		},
		batchEnter() {
			if (getApp().globalData.PROFILE == 'dev') {
				// 测试服
				uni.reLaunch({
					url: "/pages/beforeRouter/beforeRouter?shareUser=1383&subAccountId=592158&page=video&compilationId=354&fakeId=9d&enterSequence=0&shareProductUserId=909&batchId=944f9d0cd9cf404889a068e19eeb85cd2693"
				})

			} else {
				// 正式服 || QA
				uni.reLaunch({
					url: '/pages/video/video?shareUser=19610319&subAccountId=51583135&page=video&compilationId=9052&fakeId=lWkj&enterSequence=0&episodicDramaId=237171&authorId=330&shareProductUserId=18023203&batchId=baf7465931d44fe4bcbe1e4bae63511d239560&_um_ssrc=2NKQvf1IGL16407441486005AgyjjMud&_um_sts=1641278675690'
				})
			}
		},
		darenChooseClose() {
			getApp().globalData.needDarenChoose = false;
			uni.reLaunch({
				url: '../fx_xjc/fx_xjc'
			})
		},
		darenChoose1() {
			getApp().globalData.needDarenChoose = true;
			uni.reLaunch({
				url: '../fx_xjc/fx_xjc'
			})
		},
		keepBatchId() {
			getApp().globalData.spreadChannelId = this.batchIdInput
			uni.reLaunch({
				url: '../mine/mine'
			})
		},
		clearBatchId() {
			getApp().globalData.spreadChannelId = ''
			uni.reLaunch({
				url: '../mine/mine'
			})
		},
		clearCopyLinkTime() {
			uni.removeStorageSync('copyLinkTime')
			uni.showToast({
				title: '清除成功',
				icon: 'none'
			})
		}
	}
}
</script>

<style lang="scss" scoped>
.line-mess {
	display: flex;
	align-items: flex-start;
	flex-wrap: wrap;
	margin: 12rpx 0;
}

.right-box {
	flex: 1;
	word-break: break-all;
	margin-left: 24rpx;
	font-weight: bold;
}

.mask {
	width: 460rpx;
	height: 360rpx;
	border-radius: 20rpx;
	background: rgba($color: #000000, $alpha: 0.4);
	margin: 0 auto;
	position: fixed;
	left: 0;
	right: 0;
	top: 200rpx;
	bottom: 0;

}

.mask-content{
	background: #fff;
	padding-bottom: 80rpx;
}
.mask-title {
	padding: 30rpx;

}
.mask-input{
	border: 1rpx solid #ccc;
	padding: 10rpx;
}
.mask-btn {
	width: 160rpx;
	height: 50rpx;
	line-height: 50rpx;
	font-size: 28rpx;
	margin-top: 100rpx;
}
</style>
